
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>56、字符串方法中的正则应用 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="57.html" />
    
    
    <link rel="prev" href="55.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Javascript高级
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    1、值类型与引用类型及参数传递
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="1.1.html">
            
                <a href="1.1.html">
            
                    
                    1.1、值类型与引用类型的内存模型
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="2.html">
            
                <a href="2.html">
            
                    
                    2、值类型和引用类型复制的内存区
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="3.html">
            
                <a href="3.html">
            
                    
                    3、函数参数的引用传递和值传递区别
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="4.html">
            
                <a href="4.html">
            
                    
                    4、函数的arguments参数详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="5.html">
            
                <a href="5.html">
            
                    
                    5、函数不定参数个数的处理max方法实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="6.html">
            
                <a href="6.html">
            
                    
                    6、函数的参数对象化封装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="7.html">
            
                <a href="7.html">
            
                    
                    7、浏览器事件循环机制EventLoop
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" >
            
                <span>
            
                    
                    8、函数执行上下文相关概念
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="8.1.html">
            
                <a href="8.1.html">
            
                    
                    8.1、栈的数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="8.2.html">
            
                <a href="8.2.html">
            
                    
                    8.2、函数执行上下文
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="9.html">
            
                <a href="9.html">
            
                    
                    9、函数执行环境栈的变化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" >
            
                <span>
            
                    
                    10、函数执行上下文内的生命周期
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="10.1.html">
            
                <a href="10.1.html">
            
                    
                    10.1、执行上下文概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="10.2.html">
            
                <a href="10.2.html">
            
                    
                    10.2、执行上下文生命周期
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="11.html">
            
                <a href="11.html">
            
                    
                    11、变量的作用域
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="12.html">
            
                <a href="12.html">
            
                    
                    12、Javascript没有块级作用域
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="13.html">
            
                <a href="13.html">
            
                    
                    13、函数声明提升和变量声明提升
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="14.html">
            
                <a href="14.html">
            
                    
                    14、变量提升面试题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="15.html">
            
                <a href="15.html">
            
                    
                    15、变量提升面试题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="16.html">
            
                <a href="16.html">
            
                    
                    16、作用域链
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" >
            
                <span>
            
                    
                    17、函数的四种调用模式对象方法调用模式
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.18.1" data-path="17.1.html">
            
                <a href="17.1.html">
            
                    
                    17.1、方法调用模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.2" data-path="17.2.html">
            
                <a href="17.2.html">
            
                    
                    17.2、构造器调用模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.3" data-path="17.3.html">
            
                <a href="17.3.html">
            
                    
                    17.3、函数调用模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="18.html">
            
                <a href="18.html">
            
                    
                    18、函数的四种调用模式--call和apply调用模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="19.html">
            
                <a href="19.html">
            
                    
                    19、函数的四种调用模式案例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="20.html">
            
                <a href="20.html">
            
                    
                    20、函数没有重载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="21.html">
            
                <a href="21.html">
            
                    
                    21、函数的递归调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="22.html">
            
                <a href="22.html">
            
                    
                    22、递归求斐波那契
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.24" data-path="23.html">
            
                <a href="23.html">
            
                    
                    23、函数是一等公民及函数式编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.25" data-path="24.html">
            
                <a href="24.html">
            
                    
                    24、数组的map方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.26" data-path="25.html">
            
                <a href="25.html">
            
                    
                    25、数组的forEach方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.27" data-path="26.html">
            
                <a href="26.html">
            
                    
                    26、函数是对象也是函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.28" data-path="27.html">
            
                <a href="27.html">
            
                    
                    27、javascript垃圾回收
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.29" data-path="28.html">
            
                <a href="28.html">
            
                    
                    28、垃圾回收的使用场景优化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.30" data-path="29.html">
            
                <a href="29.html">
            
                    
                    29、原型链基本链接关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.31" data-path="30.html">
            
                <a href="30.html">
            
                    
                    30、构造函数的原型对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.32" data-path="31.html">
            
                <a href="31.html">
            
                    
                    31、构造函数的内部原型链
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.33" data-path="32.html">
            
                <a href="32.html">
            
                    
                    32、闭包的概念和深入理解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.34" data-path="33.html">
            
                <a href="33.html">
            
                    
                    33、匿名自执行函数与模拟块级作用域
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.35" data-path="34.html">
            
                <a href="34.html">
            
                    
                    34、循环注册dom事件典型错误及利用闭包解决
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.36" data-path="35.html">
            
                <a href="35.html">
            
                    
                    35、setTimeOut的闭包的应用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.37" data-path="36.html">
            
                <a href="36.html">
            
                    
                    36、闭包的缺点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.38" data-path="37.html">
            
                <a href="37.html">
            
                    
                    37、工厂模式及面向对象概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.39" data-path="38.html">
            
                <a href="38.html">
            
                    
                    38、工厂模式创建对象的内存分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.40" data-path="39.html">
            
                <a href="39.html">
            
                    
                    39、构造函数创建对象及缺点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.41" data-path="40.html">
            
                <a href="40.html">
            
                    
                    40、原型创建对象的模式及问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.42" data-path="41.html">
            
                <a href="41.html">
            
                    
                    41、组合构造函数模式与原型模式构建对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.43" data-path="42.html">
            
                <a href="42.html">
            
                    
                    42、稳妥构造函数模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.44" data-path="43.html">
            
                <a href="43.html">
            
                    
                    43、原型继承模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.45" data-path="44.html">
            
                <a href="44.html">
            
                    
                    44、经典组合继承模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.46" data-path="45.html">
            
                <a href="45.html">
            
                    
                    45、原型式继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.47" data-path="46.html">
            
                <a href="46.html">
            
                    
                    46、寄生继承模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.48" data-path="47.html">
            
                <a href="47.html">
            
                    
                    47、寄生组合继承模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.49" data-path="48.html">
            
                <a href="48.html">
            
                    
                    48、闭包模拟私有变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.50" data-path="49.html">
            
                <a href="49.html">
            
                    
                    49、模块化演变上
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.51" data-path="50.html">
            
                <a href="50.html">
            
                    
                    50、模块化演变下
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.52" data-path="51.html">
            
                <a href="51.html">
            
                    
                    51、正则表达式元字符和限定符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.53" data-path="52.html">
            
                <a href="52.html">
            
                    
                    52、正则贪婪模式及中括号
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.54" data-path="53.html">
            
                <a href="53.html">
            
                    
                    53、贪婪模式和非贪婪模式的补充
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.55" data-path="54.html">
            
                <a href="54.html">
            
                    
                    54、JS中正则对象及属性和test方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.56" data-path="55.html">
            
                <a href="55.html">
            
                    
                    55、正则的exec方法全解析
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.57" data-path="56.html">
            
                <a href="56.html">
            
                    
                    56、字符串方法中的正则应用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.58" data-path="57.html">
            
                <a href="57.html">
            
                    
                    57、正则高级应用日期替换执行
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.59" data-path="58.html">
            
                <a href="58.html">
            
                    
                    58、给字符串添加双引号
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.60" data-path="59.html">
            
                <a href="59.html">
            
                    
                    59、JS错误对象Error的兼容处理
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >56、字符串方法中的正则应用</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="56&#x3001;&#x5B57;&#x7B26;&#x4E32;&#x65B9;&#x6CD5;&#x4E2D;&#x7684;&#x6B63;&#x5219;&#x5E94;&#x7528;">56&#x3001;&#x5B57;&#x7B26;&#x4E32;&#x65B9;&#x6CD5;&#x4E2D;&#x7684;&#x6B63;&#x5219;&#x5E94;&#x7528;</h1>
<ul>
<li><p><strong>str.search()</strong></p>
<ul>
<li>search() &#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x68C0;&#x7D22;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x6307;&#x5B9A;&#x7684;&#x5B50;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x6216;&#x68C0;&#x7D22;&#x4E0E;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x76F8;&#x5339;&#x914D;&#x7684;&#x5B50;&#x5B57;&#x7B26;&#x4E32;</li>
<li>&#x5982;&#x679C;&#x6CA1;&#x6709;&#x627E;&#x5230;&#x4EFB;&#x4F55;&#x5339;&#x914D;&#x7684;&#x5B50;&#x4E32;&#xFF0C;&#x5219;&#x8FD4;&#x56DE; -1</li>
<li>search() &#x65B9;&#x6CD5;&#x4E0D;&#x6267;&#x884C;&#x5168;&#x5C40;&#x5339;&#x914D;&#xFF0C;&#x5B83;&#x5C06;&#x5FFD;&#x7565;&#x6807;&#x5FD7; g&#x3002;&#x5B83;&#x540C;&#x65F6;&#x5FFD;&#x7565; regexp &#x7684; lastIndex &#x5C5E;&#x6027;&#xFF0C;&#x5E76;&#x4E14;&#x603B;&#x662F;&#x4ECE;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x5F00;&#x59CB;&#x8FDB;&#x884C;&#x68C0;&#x7D22;&#xFF0C;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x5B83;&#x603B;&#x662F;&#x8FD4;&#x56DE; stringObject &#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5339;&#x914D;&#x7684;&#x4F4D;&#x7F6E;&#x3002;</li>
</ul>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript">
<span class="hljs-keyword">var</span> str=<span class="hljs-string">&quot;Visit Home!&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.search(<span class="hljs-regexp">/Home/</span>)); <span class="hljs-comment">// &#x8F93;&#x51FA; 6</span>
<span class="hljs-built_in">console</span>.log(str.search(<span class="hljs-regexp">/jake/</span>)); <span class="hljs-comment">// &#x8F93;&#x51FA; -1</span>
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
</li>
<li><p><strong>str.math()</strong></p>
<ul>
<li><p>match() &#x65B9;&#x6CD5;&#x53EF;&#x5728;&#x5B57;&#x7B26;&#x4E32;&#x5185;&#x68C0;&#x7D22;&#x6307;&#x5B9A;&#x7684;&#x503C;&#xFF0C;&#x6216;&#x627E;&#x5230;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x5339;&#x914D;</p>
<p>&#x8BE5;&#x65B9;&#x6CD5;&#x7C7B;&#x4F3C; indexOf() &#x548C; lastIndexOf()&#xFF0C;&#x4F46;&#x662F;&#x5B83;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x7684;&#x503C;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x4F4D;&#x7F6E;</p>
</li>
<li><p>&#x5982;&#x679C; regexp &#x6CA1;&#x6709;&#x6807;&#x5FD7; g&#xFF0C;&#x90A3;&#x4E48; match() &#x65B9;&#x6CD5;&#x5C31;&#x53EA;&#x80FD;&#x5728; stringObject &#x4E2D;&#x6267;&#x884C;&#x4E00;&#x6B21;&#x5339;&#x914D;&#x3002;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x627E;&#x5230;&#x4EFB;&#x4F55;&#x5339;&#x914D;&#x7684;&#x6587;&#x672C;&#xFF0C; match() &#x5C06;&#x8FD4;&#x56DE; null&#x3002;&#x5426;&#x5219;&#xFF0C;&#x5B83;&#x5C06;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#xFF0C;&#x5176;&#x4E2D;&#x5B58;&#x653E;&#x4E86;&#x4E0E;&#x5B83;&#x627E;&#x5230;&#x7684;&#x5339;&#x914D;&#x6587;&#x672C;&#x6709;&#x5173;&#x7684;&#x4FE1;&#x606F;&#x3002;&#x8BE5;&#x6570;&#x7EC4;&#x7684;&#x7B2C; 0 &#x4E2A;&#x5143;&#x7D20;&#x5B58;&#x653E;&#x7684;&#x662F;&#x5339;&#x914D;&#x6587;&#x672C;&#xFF0C;&#x800C;&#x5176;&#x4F59;&#x7684;&#x5143;&#x7D20;&#x5B58;&#x653E;&#x7684;&#x662F;&#x4E0E;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x5B50;&#x8868;&#x8FBE;&#x5F0F;&#x5339;&#x914D;&#x7684;&#x6587;&#x672C;&#x3002;&#x9664;&#x4E86;&#x8FD9;&#x4E9B;&#x5E38;&#x89C4;&#x7684;&#x6570;&#x7EC4;&#x5143;&#x7D20;&#x4E4B;&#x5916;&#xFF0C;&#x8FD4;&#x56DE;&#x7684;&#x6570;&#x7EC4;&#x8FD8;&#x542B;&#x6709;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;&#x3002;index &#x5C5E;&#x6027;&#x58F0;&#x660E;&#x7684;&#x662F;&#x5339;&#x914D;&#x6587;&#x672C;&#x7684;&#x8D77;&#x59CB;&#x5B57;&#x7B26;&#x5728; stringObject &#x4E2D;&#x7684;&#x4F4D;&#x7F6E;&#xFF0C;input &#x5C5E;&#x6027;&#x58F0;&#x660E;&#x7684;&#x662F;&#x5BF9; stringObject &#x7684;&#x5F15;&#x7528;</p>
</li>
<li><p>&#x5982;&#x679C; regexp &#x5177;&#x6709;&#x6807;&#x5FD7; g&#xFF0C;&#x5219; match() &#x65B9;&#x6CD5;&#x5C06;&#x6267;&#x884C;&#x5168;&#x5C40;&#x68C0;&#x7D22;&#xFF0C;&#x627E;&#x5230; stringObject &#x4E2D;&#x7684;&#x6240;&#x6709;&#x5339;&#x914D;&#x5B50;&#x5B57;&#x7B26;&#x4E32;&#x3002;&#x82E5;&#x6CA1;&#x6709;&#x627E;&#x5230;&#x4EFB;&#x4F55;&#x5339;&#x914D;&#x7684;&#x5B50;&#x4E32;&#xFF0C;&#x5219;&#x8FD4;&#x56DE; null&#x3002;&#x5982;&#x679C;&#x627E;&#x5230;&#x4E86;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x5339;&#x914D;&#x5B50;&#x4E32;&#xFF0C;&#x5219;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x3002;&#x4E0D;&#x8FC7;&#x5168;&#x5C40;&#x5339;&#x914D;&#x8FD4;&#x56DE;&#x7684;&#x6570;&#x7EC4;&#x7684;&#x5185;&#x5BB9;&#x4E0E;&#x524D;&#x8005;&#x5927;&#x4E0D;&#x76F8;&#x540C;&#xFF0C;&#x5B83;&#x7684;&#x6570;&#x7EC4;&#x5143;&#x7D20;&#x4E2D;&#x5B58;&#x653E;&#x7684;&#x662F; stringObject &#x4E2D;&#x6240;&#x6709;&#x7684;&#x5339;&#x914D;&#x5B50;&#x4E32;&#xFF0C;&#x800C;&#x4E14;&#x4E5F;&#x6CA1;&#x6709; index &#x5C5E;&#x6027;&#x6216; input &#x5C5E;&#x6027;&#x3002;</p>
</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> str=<span class="hljs-string">&quot;35,22,33,55&quot;</span>;
str.match(<span class="hljs-regexp">/\d{2},/g</span>); <span class="hljs-comment">// &#x6709;&#x5168;&#x5C40;g</span>
</code></pre>
<p>:herb: &#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x56FE;</p>
<p><img src="assets/1548648085743.png" alt="1548648085743"></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> str=<span class="hljs-string">&quot;35,22,33,55&quot;</span>;
str.match(<span class="hljs-regexp">/\d{2},/</span>); <span class="hljs-comment">// &#x6CA1;&#x6709;&#x5168;&#x5C40;g&#x548C;/\d{2},/.exec(str)&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x4E00;&#x6837;</span>
</code></pre>
<p>:herb: &#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x56FE;</p>
<p><img src="assets/1548648190490.png" alt="1548648190490"></p>
</li>
<li><p><strong>str.replace(regexp/substr,replacement)</strong></p>
<ul>
<li>&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x53EF;&#x4EE5;&#x662F;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x6216;&#x6B63;&#x5219;</li>
<li>&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x66FF;&#x6362;&#x4E3A;&#x7684;&#x5B57;&#x7B26;&#x4E32;</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> str = <span class="hljs-string">&apos;11,22,33&apos;</span>;
<span class="hljs-keyword">var</span> str2 = str.replace(<span class="hljs-regexp">/\d+/g</span>,<span class="hljs-string">&apos;aa&apos;</span>);
<span class="hljs-built_in">console</span>.log(); <span class="hljs-comment">// &#x7ED3;&#x679C;&#x4E3A;&#xFF1A;&quot;aa,aa,aa&quot;</span>
</code></pre>
</li>
</ul>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="55.html" class="navigation navigation-prev " aria-label="Previous page: 55、正则的exec方法全解析">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="57.html" class="navigation navigation-next " aria-label="Next page: 57、正则高级应用日期替换执行">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"56、字符串方法中的正则应用","level":"1.57","depth":1,"next":{"title":"57、正则高级应用日期替换执行","level":"1.58","depth":1,"path":"57.md","ref":"57.md","articles":[]},"previous":{"title":"55、正则的exec方法全解析","level":"1.56","depth":1,"path":"55.md","ref":"55.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"56.md","mtime":"2019-02-23T03:03:34.999Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-02-23T04:05:11.201Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

